//
//  CYWebViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/29.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYWebViewController.h"
#import <WebKit/WebKit.h>
#import "CYServiceModel.h"

@interface CYWebViewController ()
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) CYServiceModel *model;
@end

@implementation CYWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (WKWebView *)webView{
    if (!_webView) {
        _webView = [[WKWebView alloc]init];
        [self.view addSubview:_webView];
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(NAVIBAR_HEIGHT + STATUS_HEIGHT);
            make.bottom.equalTo(self.view);
            make.left.equalTo(self.view);
            make.right.equalTo(self.view);
        }];
    }
    return _webView;
}

//- (NSData *)getBodyData{
//    
//    NSArray *keys = [_postDic allKeys];
//    NSString *body = [NSString  string];
//    for (NSString *key in keys) {
//        NSString *value = _postDic[key];
//        if ([key containsString:@"url"])
//            value = [value stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//        NSString *string = [NSString stringWithFormat:@"%@=%@&",key,value];
//        body = [body stringByAppendingString:string];
//    }
//    
//    body = [body substringToIndex:body.length - 1];
//    return [body dataUsingEncoding:NSUTF8StringEncoding];
//}

- (void)setModel:(CYServiceModel *)model{
    if (_model != model) {
        _model = model;
        if (_model.link_address)
            [self showWebWiew];
        else
            [CYHUD show:@"请确定网络资源地址"];
      }
}

- (void)showWebWiew{
    NSURL *url= [NSURL URLWithString:_model.link_address];
    NSMutableURLRequest *request  = [[NSMutableURLRequest alloc]initWithURL:url];
    request.HTTPMethod = @"GET";
    [self.webView loadRequest: request];
    
}

- (void)https{
   CYHUD *hud = [[CYHUD alloc]initWithView:self.view text:@"正在加载..." HUDmode:MBProgressHUDModeIndeterminate complete:nil];
     [CYHttps POST:API_service parameters:nil success:^(id data) {
        [hud hideAnimated:YES];
        self.model = [CYServiceModel mj_objectWithKeyValues:data];
    } failure:nil];


}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self https];
}

- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationController.navigationItem.title = self.title;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//url = "https://pay.dinpay.com/gateway?input_charset=UTF-8";
//https://cp55508.com/wap/Index/home.html"

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
